refactor: split token-tracker.js into four focused modules#3343
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the 916-line containers/api-proxy/token-tracker.js into four focused modules (persistence, pure parsers, HTTP tracker, WebSocket tracker), keeping token-tracker.js as a re-export facade so all existing callers and tests continue to work unchanged. The Dockerfile COPY list is updated to include the new files.
Changes:
- Extracted log-stream lifecycle, record validation, schema constant, and
diagintotoken-persistence.js. - Extracted pure SSE/JSON parsing, decompression helpers, and
normalizeUsageintotoken-parsers.js; split tracker bodies intotoken-tracker-http.jsandtoken-tracker-ws.js. - Reduced
token-tracker.jsto a facade with identicalmodule.exports, and updated the api-proxy Dockerfile to copy the new files.
Show a summary per file
| File | Description |
|---|---|
| containers/api-proxy/token-tracker.js | Converted to a re-export facade preserving the original public API. |
| containers/api-proxy/token-persistence.js | New module: log stream, schema constant, validateTokenUsageRecord, writeTokenUsage, closeLogStream, diag. |
| containers/api-proxy/token-parsers.js | New module: pure SSE/JSON parsing, decompression helpers, normalizeUsage. |
| containers/api-proxy/token-tracker-http.js | New module: trackTokenUsage for HTTP responses. |
| containers/api-proxy/token-tracker-ws.js | New module: parseWebSocketFrames and trackWebSocketTokenUsage. |
| containers/api-proxy/Dockerfile | Adds the four new JS files to the COPY list. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 6/6 changed files
- Comments generated: 0
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@copilot address review feedback |
This comment has been minimized.
This comment has been minimized.
Addressed in a90b00d.
|
Smoke Test Results✅ GitHub API test (recent-prs.json) PASS — Claude engine validation complete
|
🔬 Smoke Test Results
Overall: PARTIAL — MCP connectivity confirmed. Pre-step template variables ( PR: refactor: split token-tracker.js into four focused modules — author
|
|
Smoke Test: FAIL Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "registry.npmjs.org"See Network Configuration for more information.
|
🏗️ Build Test Suite Results
Overall: 8/8 ecosystems passed — ✅ PASS
|
Chroot Version Comparison Results
Overall: ❌ Not all tests passed — Python and Node.js versions differ between host and chroot environments.
|
Smoke Test: GitHub Actions Services Connectivity
Overall: FAIL
|
|
Smoke test results: 1. MCP: FAIL, 2. Connectivity: FAIL, 3. File: PASS, 4. Bash: PASS. Overall: FAIL Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "localhost"See Network Configuration for more information.
|
🔬 Smoke Test: Copilot BYOK (Offline) Mode
Running in BYOK offline mode ( PR: refactor: split token-tracker.js into four focused modules — author Overall: PARTIAL (BYOK inference ✅, pre-step outputs not injected)
|
containers/api-proxy/token-tracker.jshad grown to 916 lines with four distinct concerns mixed together, making the two ~210-line tracker functions (trackTokenUsage,trackWebSocketTokenUsage) hard to review and modify in isolation.Split
token-persistence.jsvalidateTokenUsageRecord,writeTokenUsage,closeLogStream,diagtoken-parsers.jsnormalizeUsage— no I/Otoken-tracker-http.jstrackTokenUsage(HTTP response stream)token-tracker-ws.jsparseWebSocketFrames+trackWebSocketTokenUsage(WebSocket path)token-tracker.jsmodule.exportsas beforeCallsites unchanged
server.js,proxy-request.js, andwebsocket-proxy.jsall continue torequire('./token-tracker')with no modifications needed.Dockerfile
Added the four new files to the
COPYlist so they are included in the container image.